package com.example.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.entity.Skill;
import com.example.base.mapper.SkillMapper;
import com.example.base.service.SkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SkillServiceImpl implements SkillService {

    @Autowired
    SkillMapper skillMapper;

    @Override
    public IPage<Skill> selectSkill(Skill skill, Integer page, Integer size) {

        LambdaQueryWrapper<Skill> wrapper = Wrappers.lambdaQuery();

        // 类型id
        wrapper.eq(ObjectUtils.isNotEmpty(skill.getSkillId()), Skill::getSkillId, skill.getSkillId());
        // 类型名
        wrapper.eq(ObjectUtils.isNotEmpty(skill.getSkillName()), Skill::getSkillName, skill.getSkillName());

        Page<Skill> page1 = new Page<>(page, size);
        return skillMapper.selectPage(page1, wrapper);
    }

    @Override
    public List<Skill> selectAllSkill() {
        LambdaQueryWrapper<Skill> wrapper = Wrappers.lambdaQuery();
        return skillMapper.selectList(wrapper);
    }

    @Override
    public Integer addSkill(Skill skill) {
        return skillMapper.insert(skill);
    }

    @Override
    public Integer updateSkill(Skill skill) {
        return skillMapper.updateById(skill);
    }

    @Override
    public Integer delSkill(String typeId) {
        LambdaUpdateWrapper<Skill> wrapper = Wrappers.lambdaUpdate();
        // 根据id更新删除标识
        wrapper.eq(Skill::getSkillId, typeId).set(Skill::getDelSign, 1);
        return skillMapper.update(null, wrapper);
    }
}
